import {createRouter, createWebHistory} from 'vue-router'
import HomeView from '../views/HomeView.vue'
import ShowCenter from '../views/ShowCenter.vue'
import LoginView from '../views/Login.vue'
import BaseView from '../views/Base.vue'
import HostView from '../views/HostView.vue'
import Console from '../views/Console.vue'
import MultiExec from '../views/MultiExec.vue'
import Environment from "@/views/Environment"
import Release from '../views/Release.vue'
import store from '../store'
import Git from "@/views/Git";
import Jenkins from '../views/Jenkins'
import Schedule from "@/views/Schedule";
import Monitor from "@/views/Monitor";

const routes = [
    {
        meta: {
            title: 'uric自动化运维平台'
        },
        path: '/uric',
        alias: '/', // 给当前路径起一个别名
        name: 'base',
        component: BaseView,
        children: [
            {
                meta: {
                    title: '展示中心',
                    authenticate: false,
                },
                path: 'show_center',
                alias: '',
                name: 'ShowCenter',
                component: ShowCenter
            },
            {
                meta: {
                    title: '主机管理',
                    authenticate: true,
                },
                path: 'host',
                name: 'Host',
                component: HostView
            },
            {
                meta: {
                    title: 'Console',
                    authenticate: true,
                },
                path: 'console/:id',
                name: 'Console',
                component: Console
            },
            {
                path: 'multi_exec',
                name: 'MultiExec',
                component: MultiExec,
            },
            {
                path: 'environment',
                name: 'environment',
                component: Environment,
            },
            {
                path: 'release',
                name: 'release',
                component: Release,
            },
            {
                path: 'git',
                name: 'git',
                component: Git,
            },
            {
                path: 'jenkins',
                name: 'Jenkins',
                component: Jenkins,
            },
            {
                path: 'schedule',
                name: 'schedule',
                component: Schedule,
            },
            {
                path: 'monitor',
                name: 'monitor',
                component: Monitor,
            },
        ],
    },
    {
        meta: {
            title: '账户登陆',
        },
        path: '/login',
        name: 'Login',
        component: LoginView
    },
]

const router = createRouter({
    history: createWebHistory(process.env.BASE_URL),
    routes
})


// 路由守卫，主要是编写一些在页面跳转过程中， 需要自动执行的代码。例如：修改页面头部标题，验证权限。
router.beforeEach((to, from, next) => {
    // alert("页面跳转");
    document.title = to.meta.title;
    console.log("token:::", store.getters.token);
    if (to.meta.authenticate && (store.getters.token === '')) {
        // 如果访问需要登录的页面，但是没有token则默认跳转到login登陆页面
        next({name: 'Login'})
    } else {
        next()
    }
});

export default router
